<?php

namespace app\model\system;

use app\base\BaseModel;

/**
 * 模型-回收站
 * Class SysRecycleModel
 * @package app\model
 */
class SysRecycleModel extends BaseModel
{
    protected $name = 'sys_recycle';
    protected $store_id = 0;

    public function __construct(array $data = [])
    {
        parent::__construct($data);
        if (!empty($data['store_id'])) {
            $this->store_id = $data['store_id'];
        }
    }

    /**
     * 店铺id 查询范围
     * @param $query
     * @param $id
     */
    public function scopeStoreId($query, $id)
    {
        if (!empty($id)) {
            $query->alias("a");
            $query->where('a.store_id', $id);
        }
    }

    /**
     * 范围查询-keyword
     * @param $query
     * @param $keyword
     */
    public function scopeKeyword($query, $keyword)
    {
        if (!empty($keyword)) {
            $query->whereLike('desc', '%' . trim($keyword) . '%');
        }
    }

    /**
     * 还原数据
     * @param $ids
     * @throws \Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function backData($ids)
    {
        $map = [];
        if (!empty($ids)) {
            array_push($map, ['id', 'in', $ids]);
        }
        $dataList = $this
            ->field('id,model_name,path,primary_key')
            ->where($map)
            ->select();
        foreach ($dataList as $data) {
            $model_class = '\app\model\\' . $data['path'] . '\\' . $data['model_name'];
            $db_model = new $model_class();
            $cls_methods = get_class_methods($db_model);
            if (in_array('backData', $cls_methods)) {
                $flag = $db_model->backData($data['primary_key']);
            } else {
                $data_model = $db_model::onlyTrashed()->find($data['primary_key']);
                $flag = $data_model->restore();
            }
            if (false !== $flag) {
                $this->where('id', $data['id'])->delete(true);
            }
        }
    }

    /**
     * 彻底删除
     * @param $ids
     * @throws \Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delComplete($ids)
    {

        $map = [];
        if (!empty($ids)) {
            array_push($map, ['id', 'in', $ids]);
        }
        $dataList = $this
            ->storeId($this->store_id)
            ->field('id,model_name,path,primary_key')
            ->where($map)
            ->select();
        foreach ($dataList as $data) {
            $model_class = '\app\model\\' . $data['path'] . '\\' . $data['model_name'];
            $db_model = new $model_class();
            $cls_methods = get_class_methods($db_model);
            if (in_array('delComplete', $cls_methods)) {
                $flag = $db_model->delComplete($data['primary_key']);
            } else {
                $data_model = $model_class::onlyTrashed()->find($data['primary_key']);
                $flag = $data_model->force()->delete();
            }

            if (false !== $flag) {
                $this->where('id', $data['id'])->delete(true);
            }
        }
    }

    /**
     * 获取分页列表数据
     * @param $keyword
     * @param $current_page
     * @param $list_rows
     * @param string $filed
     * @return array
     */
    public function getPageData($keyword, $current_page, $list_rows, $filed = '*')
    {
        $dataList = $this
            ->field($filed)
            ->storeId($this->store_id)
            ->keyword($keyword)
            ->order('create_time', 'DESC')
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);
        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $dataList->items()];
        return $ret;
    }
}
